// pages/mockexam/special/single/detail/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    headList: [],
    headSize: 0,
    headIndex: 0,
    subjectList: [],
    subjectSize: 0,
    subjectIndex: 0,
    list: [],
    questionList: [],
    optionList: [],
    optionIndex: 0,
    listIndex: 0,
    listSize: 0,
    questionIndex: 0,
    qmaxSize: 0,
    qcount: 0,
    submitObj: {},
    flag: true,
    resultInfo: {},
    showButton: false,
    userInfo: {},
    theoryUserId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opts) {
    var options = JSON.parse(opts.dataset);
    this.setData({
      options: options
    })
    
    app.paiRequest("/app/barbecue/signup/update", {
      id: options.signup
    }).then(function (ress) {
          console.log("kkkkkkkkkskdfsf");
    })
    wx.setNavigationBarTitle({
      title: options.title,
    })
    var userInfo = app.globalData.userInfo;
    var _this = this;
    //标题
    app.paiRequest("/app/extheory/examinate/findList", {
      parentCode: options.id
    }).then(function (res) {
      var headList = res.data;
      _this.setData({
        headList: headList,
        headSize: headList.length,
        userInfo: userInfo
      })
      //题目  
      var headObj = headList[0];
      app.paiRequest("/app/extheory/examinate/findList", {
        parentCode: headObj.id
      }).then(function (ress) {
        var subjectList = ress.data;
        _this.setData({
          subjectList: subjectList
        })
        //问题questionList
        var questeObje = subjectList[0];
        app.paiRequest("/app/extheory/examinate/findList", {
          parentCode: questeObje.id
        }).then(function (resss) {
          _this.setData({
            questionList: resss.data
          })
          var optionObj = resss.data[0];
          app.paiRequest("/app/extheory/examinate/options", {
            userId: userInfo.id,
            parentCode: optionObj.id
          }).then(function (ressss) {
            _this.setData({
              optionList: ressss.data
            })
          })
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  nextquestion: function () {
    this.setData({
      questionIndex: this.data.questionIndex + 1
    })
  },
  upList: function () {
    var _this = this;
    var questionList = this.data.questionList;
    var questionIndex = this.data.questionIndex;
    var userInfo = this.data.userInfo;
    var obje = questionList[questionIndex - 1];
    if (obje == undefined) {
      var subjectList = this.data.subjectList;
      var subjectIndex = this.data.subjectIndex;
      if (subjectList[subjectIndex - 1] == undefined) {
        var headList = this.data.headList;
        var headIndex = this.data.headIndex;
        if (headList[headIndex - 1] == undefined) {

        } else {
          this.setData({
            headIndex: headIndex - 1
          })
          app.paiRequest("/app/extheory/examinate/findList", {
            parentCode: headList[headIndex - 1].id
          }).then(function (res) {
            var subjectList = res.data;
            _this.setData({
              subjectList: subjectList,
              subjectIndex: 0
            })
            app.paiRequest("/app/extheory/examinate/findList", {
              parentCode: subjectList[0].id
            }).then(function (ress) {
              var questionList = ress.data;
              _this.setData({
                questionList: questionList,
                questionIndex: 0
              })
              app.paiRequest("/app/extheory/examinate/options", {
                userId: userInfo.id,
                parentCode: questionList[0].id
              }).then(function (resss) {
                _this.setData({
                  optionList: resss.data
                })
              })
            })
          })
        }
      } else {
        this.setData({
          subjectIndex: subjectIndex - 1
        })
        app.paiRequest("/app/extheory/examinate/findList", {
          parentCode: subjectList[subjectIndex - 1].id
        }).then(function (res) {
          var questionList = res.data;
          _this.setData({
            questionList: questionList,
            questionIndex: 0
          })
          app.paiRequest("/app/extheory/examinate/options", {
            userId: userInfo.id,
            parentCode: questionList[0].id
          }).then(function (ress) {
            _this.setData({
              optionList: ress.data
            })
          })
        })
      }
    } else {
      var optionIndex = _this.data.optionIndex;
      this.setData({
        questionIndex: questionIndex - 1
      })

      app.paiRequest("/app/extheory/examinate/options", {
        userId: userInfo.id,
        parentCode: questionList[questionIndex - 1].id
      }).then(function (ress) {
        _this.setData({
          optionList: ress.data
        })
      })
    }
  },
  nextList: function () {
    var _this = this;
    var questionList = this.data.questionList;
    var questionIndex = this.data.questionIndex;
    var obje = questionList[questionIndex + 1];
    var userInfo = _this.data.userInfo;
    if (obje == undefined) {
      var subjectList = this.data.subjectList;
      var subjectIndex = this.data.subjectIndex;
      if (subjectList[subjectIndex + 1] == undefined) {
        var headList = this.data.headList;
        var headIndex = this.data.headIndex;
        if (headList[headIndex + 1] == undefined) {
          this.setData({
            showButton: true
          })
        } else {
          
          app.paiRequest("/app/extheory/examinate/findList", {
            parentCode: headList[headIndex + 1].id
          }).then(function (res) {
            var subjectList = res.data;
            _this.setData({
              subjectList: subjectList,
              subjectIndex: 0,
              headIndex: headIndex + 1
            })
            app.paiRequest("/app/extheory/examinate/findList", {
              parentCode: subjectList[0].id
            }).then(function (ress) {
              var questionList = ress.data;
              _this.setData({
                questionList: questionList,
                questionIndex: 0
              })
              app.paiRequest("/app/extheory/examinate/options", {
                userId: userInfo.id,
                parentCode: questionList[0].id
              }).then(function (resss) {
                _this.setData({
                  optionList: resss.data
                })
              })
            })
          })
        }

      } else {
        
        app.paiRequest("/app/extheory/examinate/findList", {
          parentCode: subjectList[subjectIndex + 1].id
        }).then(function (res) {
          var questionList = res.data;
          _this.setData({
            questionList: questionList,
            questionIndex: 0,
            subjectIndex: subjectIndex + 1
          })
          app.paiRequest("/app/extheory/examinate/options", {
            userId: userInfo.id,
            parentCode: questionList[0].id
          }).then(function (ress) {
            _this.setData({
              optionList: ress.data
            })
          })
        })
      }
    } else {
      
      app.paiRequest("/app/extheory/examinate/options", {
        userId: userInfo.id,
        parentCode: questionList[questionIndex + 1].id
      }).then(function (res) {
        _this.setData({
          optionList: res.data,
          questionIndex: questionIndex + 1
        })
      })
    }
  },
  radioChang: function (e) {
    var questionId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var answerId = e.detail.value;
    var submitObj = this.data.submitObj;
    submitObj[questionId] = answerId;
    this.setData({
      submitObj: submitObj
    })
    var userInfo = this.data.userInfo;
    var options = this.data.options;
    app.paiRequest("/app/examination/barbecue/record/add", {
      theoryId: options.id,
      userId: userInfo.id,
      questionId: questionId,
      answerId: answerId,
      questionType: type
    }).then(function (res) {
      console.log(res)
    })
  },
  submitQuestion: function () {
    var submitObj = this.data.submitObj;
    var _this = this;
    var userInfo = this.data.userInfo;
    var options = this.data.options;
    var theoryUserId = this.data.theoryUserId;
    app.paiRequest("/app/extheory/examinate/compute", { theoryUserId: theoryUserId, userId: userInfo.id, theoryId: options.id, signupId: options.signup }).then(function (res) {
      console.log(res)
      var data = res.data;
      _this.setData({
        resultInfo: data,
        flag: false
      })
    })
  },
  // 遮罩层隐藏
  conceal: function () {
    this.setData({
      flag: true
    })
  },
  deleteRecord: function () {
    wx.navigateBack({
      delta:1
    })
  }
})